package com.rui.dao.impl;

import com.rui.dao.SubjectDao;
import com.rui.entity.Subject;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */
@Repository
public class SubjectDAOImpl implements SubjectDao {
    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    public void add(Subject subject) {
        getSession().beginTransaction();
        getSession().save(subject);
        getSession().getTransaction().commit();
    }

    public void delete(Integer id) {
        getSession().beginTransaction();
        Subject subject = (Subject) getSession().get(Subject.class, Integer.valueOf(id));
        if (subject != null) {
            getSession().delete(subject);
        }
        getSession().getTransaction().commit();
    }

    public void update(Subject subject) {
        getSession().beginTransaction();
        getSession().update(subject);
        getSession().getTransaction().commit();
    }

    public Subject findOne(Integer id) {
        getSession().beginTransaction();
        Subject subject = (Subject) getSession().get(Subject.class, Integer.valueOf(id));
        getSession().getTransaction().commit();
        return subject;
    }

    public List<Subject> findSubjects(String name, Integer pageSize, Integer pageIndex) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Subject where 1 = 1");
        if (StringUtils.isNotBlank(name)) {
            sb.append(" and name like '%" + name + "%'");
        }
        Query query = getSession().createQuery(sb.toString());
        query.setFirstResult((pageIndex - 1) * pageSize);
        query.setMaxResults(pageSize);
        List<Subject> subjects = query.list();
        getSession().getTransaction().commit();
        return subjects;
    }

    public Integer findTotalPage(String name, Integer pageSize) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("select count(id) from Subject where 1 = 1");
        if (StringUtils.isNotBlank(name)) {
            sb.append(" and name like '%" + name + "%'");
        }
        Query query = getSession().createQuery(sb.toString());
        Long count = (Long) query.uniqueResult();
        getSession().getTransaction().commit();
        if (count % pageSize == 0) {
            return count.intValue() / pageSize;
        } else {
            return count.intValue() / pageSize + 1;
        }
    }

    public Subject findByName(String name) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Subject where name = '" + name + "'");
        Query query = getSession().createQuery(sb.toString());
        List<Subject> subjects = query.list();
        getSession().getTransaction().commit();
        if (subjects.size() > 0) {
            return subjects.get(0);
        } else {
            return null;
        }
    }

    public List<Subject> findSubjects(String name, Integer id) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Subject where 1 = 1");
        if (StringUtils.isNotBlank(name)) {
            sb.append(" and name = '" + name + "'");
        }
        if (id != null) {
            sb.append(" and id = " + id);
        }
        Query query = getSession().createQuery(sb.toString());
        List<Subject> subjects = query.list();
        getSession().getTransaction().commit();
        return subjects;
    }
}
